#include "treenode.h"
/**
 * Note: The returned array must be malloced, assume caller calls free().
 */

void helper(const struct Node *root, int *res, int *pos)
{
    if (NULL == root)
    {
        return;
    }
    for (int i = 0; i < root->numChildren; i++)
    {
        helper(root->children[i], res, pos);
    }
    res[(*pos)++] = root->val;
}

int *postorder(struct Node *root, int *returnSize)
{
    const int MAX_NODE_NUM = 10000;
    int *res = (int *)malloc(sizeof(int) * MAX_NODE_NUM);
    int pos = 0;
    helper(root, res, &pos);
    *returnSize = pos;
    return res;
}